package com.iyao_app.adapter;

import android.graphics.Color;
import android.net.Uri;
import android.support.annotation.Nullable;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.StaggeredGridLayoutManager;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.BaseViewHolder;
import com.facebook.drawee.backends.pipeline.Fresco;
import com.facebook.drawee.backends.pipeline.PipelineDraweeController;
import com.facebook.drawee.view.SimpleDraweeView;
import com.facebook.imagepipeline.common.ResizeOptions;
import com.facebook.imagepipeline.request.ImageRequest;
import com.facebook.imagepipeline.request.ImageRequestBuilder;
import com.iyao_app.R;
import com.iyao_app.entry.ResultModel;
import com.iyao_app.entry.StoryDetail;
import com.iyao_app.entry.User;
import com.iyao_app.fragment.FindFragment;
import com.iyao_app.http.NetCallBack;
import com.iyao_app.http.NetUtils;
import com.iyao_app.util.Contants;
import com.iyao_app.util.CountUtils;
import com.iyao_app.util.DbUtil;
import com.iyao_app.util.ResourcesUtils;
import com.iyao_app.util.RxHeartLayout_;
import com.iyao_app.util.TimeFormat;
import com.iyao_app.view.OnImageClickListener;
import com.iyaowoqu.base_library.util.LogUtil;

import java.util.List;
import java.util.Random;

import butterknife.BindView;
import butterknife.ButterKnife;
import io.github.rockerhieu.emojicon.EmojiconTextView;

/**
 * Created by tongxue on 2017/11/30.
 */

public class FindAttentionAdapter extends BaseQuickAdapter<StoryDetail, FindAttentionAdapter.FindAttention> {

    private PicturesAdapter picturesAdapter;
    OnImageClickListener cc;
    private int i1;

    final int[] image = {R.mipmap.ic_a, R.mipmap.ic_b, R.mipmap.ic_c, R.mipmap.ic_d, R.mipmap.ic_e, R.mipmap.ic_f, R.mipmap.ic_g, R.mipmap.ic_h, R.mipmap.ic_i, R.mipmap.ic_j, R.mipmap.ic_k};

    private Random random = new Random();

    public FindAttentionAdapter(@Nullable List<StoryDetail> data, OnImageClickListener findonclick) {
        super(R.layout.item_find_attention, data);
        cc = findonclick;
    }

    @Override
    protected void convert(final FindAttention holder, final StoryDetail item) {

        StaggeredGridLayoutManager staggeredGridLayoutManager = null;
        if (item.eventFileUrlList.size() == 1) {
            staggeredGridLayoutManager = new StaggeredGridLayoutManager(1, LinearLayoutManager.VERTICAL);
        } else {
            staggeredGridLayoutManager = new StaggeredGridLayoutManager(3, LinearLayoutManager.VERTICAL);
        }
        try {
            Uri uri = Uri.parse(item.avatarUrl);
            ImageRequest request = ImageRequestBuilder.newBuilderWithSource(uri)
                    .setResizeOptions(new ResizeOptions(100, 100))
                    .build();
            PipelineDraweeController controller = (PipelineDraweeController) Fresco.newDraweeControllerBuilder()
                    .setOldController(holder.sdvHead.getController())
                    .setImageRequest(request)
                    .build();
            holder.sdvHead.setController(controller);
        } catch (Exception e) {
            e.printStackTrace();
        }


        holder.tvName.setText(item.nickname);
        holder.tvLocation.setText(item.sceneName);
        holder.tvText.setText(item.eventDesc);
        holder.tvGuanzhu.setText(TimeFormat.getTime(item.createTime));
        holder.rvmHorizontal.setLayoutManager(staggeredGridLayoutManager);
        holder.itemView.setLayoutParams(new RecyclerView.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT));
        picturesAdapter = new PicturesAdapter(holder.rvmHorizontal, item.eventFileUrlList, cc);
        holder.rvmHorizontal.setAdapter(picturesAdapter);
        holder.tvLike.setText(String.format(CountUtils.getCount(item.goodCount)));
        holder.tvComment.setText(String.format(CountUtils.getCount(item.commentCount)));
        holder.tvZhuanfa.setText("");
        holder.addOnClickListener(R.id.tv_like);
        holder.addOnClickListener(R.id.tv_comment);
        holder.addOnClickListener(R.id.tv_zhuanfa);
        item.count = Integer.parseInt(item.goodCount);
        holder.mLinLike.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                LogUtil.e("===喜欢===");
                like(item, holder.tvLike, holder.mIvLike);

                holder.rxHeartLayout.post(new Runnable() {
                    @Override
                    public void run() {
                        i1 = random.nextInt(image.length);
                        int rgb = Color.rgb(random.nextInt(255), random.nextInt(255), random.nextInt(255));
                        holder.rxHeartLayout.addHeart(rgb, image[i1], image[i1]);
                    }
                });
            }
        });


    }

    private void like(final StoryDetail item, final TextView tv, final ImageView iv) {
        User user = DbUtil.getUser();
        NetUtils.getInstance().goodOrBad(item.id, user.id, new NetCallBack() {
            @Override
            public void onSuccess(String message, int code, String response, ResultModel model) {
                if (code == Contants.SUCCESS_CODE) {
                    item.count += 1;
                    item.goodCount = String.valueOf(item.count);
                    item.goodVote = false;
                    tv.setText(item.goodCount);
                    tv.setTextColor(ResourcesUtils.getColor(R.color.app_orange_ff773d));
                    iv.setImageResource(R.mipmap.btn_wuxianxihuan_lager);
                }

            }

            @Override
            public void onFail(String message, int code) {

            }
        }, null);
    }

    class FindAttention extends BaseViewHolder {
        @BindView(R.id.sdv_head)
        SimpleDraweeView sdvHead;
        @BindView(R.id.tv_name)
        EmojiconTextView tvName;
        @BindView(R.id.tv_location)
        TextView tvLocation;
        @BindView(R.id.tv_guanzhu)
        TextView tvGuanzhu;
        @BindView(R.id.rvm_horizontal)
        RecyclerView rvmHorizontal;
        @BindView(R.id.tv_like)
        TextView tvLike;
        @BindView(R.id.tv_comment)
        TextView tvComment;
        @BindView(R.id.tv_zhuanfa)
        TextView tvZhuanfa;
        @BindView(R.id.tv_text)
        TextView tvText;
        @BindView(R.id.coordinatorLayout)
        LinearLayout mCoordinatorLayout;
        @BindView(R.id.heart_layout)
        RxHeartLayout_ rxHeartLayout;
        @BindView(R.id.iv_like)
        ImageView mIvLike;
        @BindView(R.id.lin_like)
        LinearLayout mLinLike;

        public FindAttention(View itemView) {
            super(itemView);
            ButterKnife.bind(this, itemView);
        }
    }
}
